package com.sherlockHolmes.init.base.listener;

import com.sherlockHolmes.init.base.Enum.VIP;
import com.sherlockHolmes.init.base.server.WebSocketServer;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//@Component
public class RedisKeyExpirationListener  extends KeyExpirationEventMessageListener {

    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private WebSocketServer webSocketServer;

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String s = message.toString();
        //监听过期的Key
        if(s.contains(VIP.VIP_YEAR.toString())){
            webSocketServer.sendMessageBySys(s.replace(VIP.VIP_YEAR.toString(),""),"你的年度会员已到期");
        }
        if(s.contains(VIP.VIP_MONTH.toString())){
            webSocketServer.sendMessageBySys(s.replace(VIP.VIP_MONTH.toString(),""),"你的月度会员已到期");
        }
        if(s.contains(VIP.VIP_DAY.toString())){
            webSocketServer.sendMessageBySys(s.replace(VIP.VIP_DAY.toString(),""),"你的日度会员已到期");
        }
        //删除过期的key
        redisTemplate.delete(s);
    }
}


